package MyFragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.qb.devtest.MainActivity;
import com.qb.devtest.MyApplication;
import com.qb.devtest.R;

import org.w3c.dom.Text;

import utils.MyUtil;

public class FragmentScan extends Fragment {
    Context ctx = MyApplication.getInstance();
    EditText etScan,etFocus;
    TextView tvScan,tvLocale;
    static InputMethodManager imm;
    Boolean scanComplete = true;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scan, container, false);
        etScan = view.findViewById(R.id.et_scan);
        etFocus = view.findViewById(R.id.et_focus);
        tvScan = view.findViewById(R.id.tv_scan);
        tvLocale = view.findViewById(R.id.tv_locale);
        imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);

        etFocus.requestFocus();
        etFocus.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 文本更改前的回调
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 文本更改时的回调
                // 这里可以获取EditText中的实时文本
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 文本更改后的回调
                // 这里也可以获取EditText中的最终文本
                etScan.setText(s, null);
            }
        });
        etFocus.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return scanResultDone(v,actionId,event);
            }
        });
        etScan.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                etFocus.requestFocus();
                return scanResultDone(v,actionId,event);
            }
        });

        Button btClear = view.findViewById(R.id.btn_scan_clear);
        btClear.setOnClickListener(view1 -> {
            etScan.setText("", null);
            tvScan.setText("");
            etFocus.setText("");
            etFocus.requestFocus();
            hideSoftInputFromWindow(ctx, etScan);
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 在这里初始化你的Fragment视图组件
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hideSoftInputFromWindow(ctx, etScan);
    }

    //收起软键盘
    public static void hideSoftInputFromWindow(Context ctx,EditText et) {
        if (imm != null) {
            imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
        }
    }

    // 扫码以回车作为结束的处理
    private boolean scanResultDone(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE ||
                actionId == EditorInfo.IME_ACTION_GO ||
                (event != null && event.getAction() == KeyEvent.ACTION_DOWN &&
                        event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
            if (v.getId()==R.id.et_focus) {
                String str = etScan.getText()+"\n"+tvScan.getText();
                tvScan.setText(str.replace("\n\n", "\n"));
                etFocus.setText("", null);
                etFocus.requestFocus();
                etScan.setText("", null);
                hideSoftInputFromWindow(ctx, etScan);
                return true;
            }
            hideSoftInputFromWindow(ctx, etScan);
            return false;
        } else {
            return false;
        }
    }
}